<template>
  <div
    v-if="isExternal"
    :style="styleExternalIcon"
    class="svg-external-icon svg-icon"
    v-on="$listeners"
  />
  <svg
    v-else
    :class="svgClass"
    aria-hidden="true"
    v-on="$listeners"
  >
    <use :xlink:href="iconName" />
  </svg>
</template>

<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator'
import { isExternal } from '@/utils/validate'

@Component({
  name: 'SvgIcon'
})
export default class extends Vue {
  @Prop({ required: true }) private iconClass!:string;
  @Prop() private className!:string ;

  get isExternal() {
    return isExternal(this.iconClass)
  }
  get iconName() {
    return `#icon-${this.iconClass}`
  }
  get svgClass() {
    if (this.className) {
      return 'svg-icon ' + this.className
    } else {
      return 'svg-icon'
    }
  }
  get styleExternalIcon() {
    return {
      mask: `url(${this.iconClass}) no-repeat 50% 50%`,
      '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
    }
  }
}

</script>

<style scoped>
.svg-icon {
  width: 16px;
  height: 16px;
  vertical-align: -0.15em !important;
  fill: currentColor;
  overflow: hidden;
}
/* svg:last-of-type { fill: red; } */

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}
</style>
